# Replication for Figures 1 and 2 of Banda and Carsey 2015, "Two-stage elections, strategic
# candidates, and campaign agendas"


rm(list=ls(all=TRUE))
library(gplots)


# Figure 1

# One contested primary (these are LRM values from the paper)

# Democratic issues, sponsor
spon.demissue.prim.lrm <- .4940708
spon.demissue.gen.lrm <- -.074976

# Democratic issues, primary opponent
prim.demissue.spon.lrm <- .6841433
prim.demissue.gen.lrm <- .3856246

# Democratic issues, general opponent
gen.demissue.spon.lrm <- .0028985
gen.demissue.prim.lrm <- .0449466

# Republican issues, sponsor
spon.repissue.prim.lrm <- .5793661
spon.repissue.gen.lrm <- .175771

# Republican issues, primary opponent
prim.repissue.spon.lrm <- .4239878
prim.repissue.gen.lrm <- .2662616

# Republican issues, general opponent
gen.repissue.spon.lrm <- .2426761
gen.repissue.prim.lrm <- .0961253

# Democratic issues
dem.iss.1 <- as.vector(c(spon.demissue.prim.lrm*100,spon.demissue.gen.lrm*100))
dem.iss.2 <- as.vector(c(prim.demissue.spon.lrm*100,prim.demissue.gen.lrm*100))
dem.iss.3 <- as.vector(c(gen.demissue.spon.lrm*100,gen.demissue.prim.lrm*100))
dem.iss <- cbind(dem.iss.1,dem.iss.2,dem.iss.3)

# Republican issues
rep.iss.1 <- as.vector(c(spon.repissue.prim.lrm*100,spon.repissue.gen.lrm*100))
rep.iss.2 <- as.vector(c(prim.repissue.spon.lrm*100,prim.repissue.gen.lrm*100))
rep.iss.3 <- as.vector(c(gen.repissue.spon.lrm*100,gen.repissue.prim.lrm*100))
rep.iss <- cbind(rep.iss.1,rep.iss.2,rep.iss.3)

############
### Plot ###
############

barplot2(dem.iss, beside = TRUE, ylab = "Change in # of ads mentioning Democratic-owned issues",
         main='Democratic-owned issues',ylim=c(-10,70),col=c('gray80','white','black','white','black','gray80'),
         plot.grid=TRUE,grid.col='gray46',grid.inc=15,
         names.arg=c('Contested primary winners','Contested primary losers','Uncontested primary winners'))
legend('topright',labels<-c("Contested primary winners",'Contested primary losers',
                           'Uncontested primary winners'),fill=c("black",'gray80','white'),
       bty='y',cex=.8,bg='white')

barplot2(rep.iss, beside = TRUE, ylab = "Change in # of ads mentioning Republican-owned issues",
         main='Republican-owned issues',ylim=c(-10,70),col=c('gray80','white','black','white','black','gray80'),
         plot.grid=TRUE,grid.col='gray46',grid.inc=15,
         names.arg=c('Contested primary winners','Contested primary losers','Uncontested primary winners'))
legend('topright',labels<-c("Contested primary winners",'Contested primary losers',
                            'Uncontested primary winners'),fill=c("black",'gray80','white'),
       bty='y',cex=.8,bg='white')





# Figure 2

# Two contested primaries (these are LRM values from the paper)

# Democratic issues, dem win
demwin.demissue.demlose.lrm <- .9105085
demwin.demissue.repwin.lrm <- .3393027
demwin.demissue.replose.lrm <-  -.4559719

# Democratic issues, dem lose
demlose.demissue.demwin.lrm <- .6045365
demlose.demissue.repwin.lrm <- .1576677
demlose.demissue.replose.lrm <-  .6374271

# Democratic issues, rep win
repwin.demissue.demwin.lrm <- .1646489
repwin.demissue.demlose.lrm <- -.0702474
repwin.demissue.replose.lrm <- .5432465

# Democratic issues, rep lose
replose.demissue.demwin.lrm <- .1488763
replose.demissue.demlose.lrm <- .1340901
replose.demissue.repwin.lrm <- .0992544

# Republican issues, dem win
demwin.repissue.demlose.lrm <- .6484006
demwin.repissue.repwin.lrm <- .1185228
demwin.repissue.replose.lrm <-  -.1740065

# Republican issues, dem lose
demlose.repissue.demwin.lrm <- .5183186
demlose.repissue.repwin.lrm <- -.0969387
demlose.repissue.replose.lrm <- .1855502 

# Republican issues, rep win
repwin.repissue.demwin.lrm <- .508002
repwin.repissue.demlose.lrm <- -.4467661
repwin.repissue.replose.lrm <- .9597759

# Republican issues, rep lose
replose.repissue.demwin.lrm <- -.0964572
replose.repissue.demlose.lrm <- .4924061
replose.repissue.repwin.lrm <- .9930253

# Democratic issues
dem.iss.1 <- as.vector(c(demwin.demissue.demlose.lrm*100,demwin.demissue.repwin.lrm*100,
                         demwin.demissue.replose.lrm*100))
dem.iss.2 <- as.vector(c(demlose.demissue.demwin.lrm*100,demlose.demissue.repwin.lrm*100,
                         demlose.demissue.replose.lrm*100))
dem.iss.3 <- as.vector(c(repwin.demissue.demwin.lrm*100,repwin.demissue.demlose.lrm*100,
                         repwin.demissue.replose.lrm*100))
dem.iss.4 <- as.vector(c(replose.demissue.demwin.lrm*100,replose.demissue.demlose.lrm*100,
                         replose.demissue.repwin.lrm*100))
dem.iss4 <- cbind(dem.iss.1,dem.iss.2,dem.iss.3,dem.iss.4)

# Republican issues
rep.iss.1 <- as.vector(c(demwin.repissue.demlose.lrm*100,demwin.repissue.repwin.lrm*100,
                         demwin.repissue.replose.lrm*100))
rep.iss.2 <- as.vector(c(demlose.repissue.demwin.lrm*100,demlose.repissue.repwin.lrm*100,
                         demlose.repissue.replose.lrm*100))
rep.iss.3 <- as.vector(c(repwin.repissue.demwin.lrm*100,repwin.repissue.demlose.lrm*100,
                         repwin.repissue.replose.lrm*100))
rep.iss.4 <- as.vector(c(replose.repissue.demwin.lrm*100,replose.repissue.demlose.lrm*100,
                         replose.repissue.repwin.lrm*100))
rep.iss4 <- cbind(rep.iss.1,rep.iss.2,rep.iss.3,rep.iss.4)

############
### Plot ###
############

barplot2(dem.iss4, beside = TRUE, ylab = "Change in # of ads mentioning Democratic-owned issues",
         main='Democratic-owned issues',ylim=c(-50,100),col=c('white','black','gray40',
         'gray80','black','gray40','gray80','white','gray40','gray80','white','black'),
         plot.grid=TRUE,grid.col='gray46',grid.inc=16,
         names.arg=c('Democratic winners','Democratic losers','Republican winners',
                     'Republican losers'))
legend('topright',labels<-c("Democratic winners",'Democratic losers',
                            'Republican winners','Republican losers'),
       fill=c("gray80",'white','black','gray40'),bty='y',cex=1,bg='white')


barplot2(rep.iss4, beside = TRUE, ylab = "Change in # of ads mentioning Republican-owned issues",
         main='Republican-owned issues',ylim=c(-50,100),col=c('white','black','gray40',
                                                              'gray80','black','gray40','gray80','white','gray40','gray80','white','black'),
         plot.grid=TRUE,grid.col='gray46',grid.inc=16,
         names.arg=c('Democratic winners','Democratic losers','Republican winners',
                     'Republican losers'))
legend('topleft',labels<-c("Democratic winners",'Democratic losers',
                            'Republican winners','Republican losers'),
       fill=c("gray80",'white','black','gray40'),bty='y',cex=1,bg='white',inset=c(.01,0))